const uploading = (url, row, formData, callback, cancel) => {
  row.status = 'uploading';
  axiosUpload(
    url,
    formData,
    function (percentage) {
      if (percentage < 100) {
        row.status = 'uploading';
        row.percentage = percentage;
      } else {
        row.status = 'server';
      }
    },
    function (c) {
      cancel && cancel(c);
    }
  )
    .then((res) => {
      if (res.code === 200) {
        row.status = 'success';
      } else {
        row.status = 'error';
      }
      callback && callback(res);
    })
    .catch((err) => {
      row.status = 'error';
    });
};

// 上传状态
const uploadStatus = (row) => {
  switch (row.status) {
    case 'ready':
      return 'default';
    case 'uploading':
    case 'server':
      return 'primary';
    case 'success':
      return 'success';
    case 'error':
      return 'danger';
    default:
      return 'warning';
  }
};
// 上传文本
const uploadText = (row) => {
  switch (row.status) {
    case 'ready':
      return '读取状态';
    case 'uploading':
      return '上传中（' + row.percentage + '%)';
    case 'server':
      return '处理中';
    case 'success':
      return '上传成功';
    case 'error':
      return '上传失败';
    case "canceled":
      return "取消上传";
    default:
      return '未知状态';
  }
};

/**
 * 格式化文件大小
 * @param value
 * @return string
 */
function renderFileSize(value) {
  if (null === value || value === '') {
    return '0 Bytes';
  }
  const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  let index,
    srcsize = parseFloat(value);
  index = Math.floor(Math.log(srcsize) / Math.log(1024));
  let size = srcsize / Math.pow(1024, index);
  //  保留的小数位数
  size = size.toFixed(2);
  return size + unitArr[index];
}

// 解析文件
function parseFile(file) {
  const name = file.name;
  const tIndex = name.lastIndexOf('.');
  const type = name.substring(tIndex + 1, name.length);
  const filename = name.substring(0, tIndex);
  return { filename, type };
}

// 读取本地文件
let fileForm;
let fileInput;
const readFile = function (options) {
  const opts = Object.assign({}, options);
  if (!fileForm) {
    fileForm = document.createElement('form');
    fileForm.className = 'wangbuhao--file-form';
    fileInput = document.createElement('input');
    fileInput.name = 'file';
    fileInput.type = 'file';
    fileForm.appendChild(fileInput);
    document.body.appendChild(fileForm);
  }
  return new Promise(function (resolve, reject) {
    const types = opts.types || [];
    const isAllType = !types.length || types.some((type) => type === '*');
    fileInput.multiple = !!opts.multiple;
    fileInput.accept = isAllType ? '' : '.'.concat(types.join(', .'));
    fileInput.onchange = function (evnt) {
      let fIndex
      let files = evnt.target.files;
      const file = files[0];
      let errType = [];
      // 存储更新所选文件
      let curFiles = [];
      // 文件大小校验
      if (opts.maxSize && opts.maxSize > 0) {
        for (fIndex = 0; fIndex < files.length; fIndex++) {
          const file = files[fIndex];
          if (file.size / 1024 > opts.maxSize) {
            errType.push(1);
          } else {
            curFiles.push(file);
          }
        }
        files = curFiles;
        if (errType && errType.length > 0) {
          if (opts.message !== false) {
            $baseMsg('文件大小最大为' + opts.maxSize + 'KB， 已超出限制');
          }
        }
        errType = [];
      }
      // 校验类型
      if (!isAllType) {
        curFiles = [];
        for (fIndex = 0; fIndex < files.length; fIndex++) {
          var type = parseFile(files[fIndex]).type;
          if (types.includes(type)) {
            curFiles.push(files[fIndex]);
          } else {
            errType.push(type);
          }
        }
        files = curFiles;
        if (errType && errType.length > 0) {
          if (opts.message !== false) {
            $baseMsg('不支持的文件类型：' + errType);
          }
        }
      }
      resolve({ status: true, files: files, file: file });
    };
    fileForm.reset();
    fileInput.click();
  });
};
